package com.ctshk.rpc.scenic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.scenic.dto.AirshowProductDTO;
import com.ctshk.rpc.scenic.dto.ScenicProductListDTO;
import com.ctshk.rpc.scenic.entity.AirshowDistributorSetting;
import com.ctshk.rpc.scenic.entity.AirshowProduct;
import com.ctshk.rpc.scenic.mapper.AirshowProductMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.rpc.scenic.req.AirshowProductListReq;
import com.ctshk.rpc.scenic.req.AirshowProductReq;
import com.ctshk.rpc.scenic.service.IAirshowProductService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-06-10
 */
@DubboService
public class AirshowProductServiceImpl extends ServiceImpl<AirshowProductMapper, AirshowProduct> implements IAirshowProductService {

    @Autowired
    private AirshowProductMapper airshowProductMapper;

    @Override
    public Result addProduct(AirshowProductReq airshowProductReq, TokenUser currentUser) {

        AirshowProduct airshowProduct = EntityUtil.copy(airshowProductReq, AirshowProduct.class);

        long id = SnowflakeIdWorker.nextId();

        airshowProduct.setId(id);
        airshowProduct.setIsEnable(1);
        airshowProduct.setCreateId(currentUser.getId());
        airshowProduct.setIsDeleted(0);

        int insert = airshowProductMapper.insert(airshowProduct);

        if(insert > 0)

            return Result.success(id);

        throw new BusinessException(SystemError.SYS_437);
    }

    @Override
    public Result<AirshowProductDTO> getProductById(Long id) {

        AirshowProduct airshowProduct = airshowProductMapper.selectById(id);

        if(airshowProduct == null){
            throw new BusinessException(SystemError.SYS_433,"产品不存在");
        }

        AirshowProductDTO copy = EntityUtil.copy(airshowProduct, AirshowProductDTO.class);

        return Result.success(copy);
    }

    @Override
    public Result updateProduct(AirshowProductReq airshowProductReq,TokenUser currentUser) {

        if(airshowProductReq.getId() == null){
            throw new BusinessException(SystemError.SYS_412);
        }

        AirshowProduct airshowProduct = EntityUtil.copy(airshowProductReq, AirshowProduct.class);

        int i = airshowProductMapper.updateById(airshowProduct);

        if(i > 0)

            return Result.success(airshowProductReq.getId());

        throw new BusinessException(SystemError.SYS_437);
    }

    @Override
    public PageResponse<AirshowProductDTO> getProductListByParams(AirshowProductListReq airshowProductReq) {

        QueryWrapper<AirshowProduct> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(airshowProductReq.getProductName())){
            queryWrapper.like("product_name",airshowProductReq.getProductName());
        }
        queryWrapper.eq("is_deleted",0);

        Page<AirshowProduct> iPage = new Page<>(airshowProductReq.getPageNo(), airshowProductReq.getPageSize());

        iPage = airshowProductMapper.selectPage(iPage,queryWrapper);

        List<AirshowProductDTO> airshowProductDTOList = EntityUtil.copyList(iPage.getRecords(), AirshowProductDTO.class);

        PageResponse<AirshowProductDTO> result = new PageResponse<>(airshowProductDTOList, iPage.getCurrent() == 1,
                iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());

        return result;
    }
}
